<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form
        ref="listQuery"
        :model="listQuery"
        :inline="true"
        label-position="top"
        @keydown.enter.native="handleFilter"
      >
        <search-item
          v-model="listQuery.userAccount"
          prop="userAccount"
          label="姓名"
        ></search-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >
          查询
        </el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-refresh-right"
          @click="handleReset"
        >
          重置
        </el-button>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width:50%"
    >
      <el-table-column
        prop="id"
        label="序号"
        type="index"
        align="center"
        width="80"
      >
      </el-table-column>
      <el-table-column prop="name" label="姓名" align="center">
        <template v-slot="{ row }">
          <span>{{ row.userAccount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="{ row, $index }">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row, $index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :current.sync="listQuery.current"
      :size.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { getUserList, deleteUser } from "@/api/user";
import Button from "@/mixin/ButtonMixin.js";
import Pagination from "@/components/Pagination";
export default {
  name: "setmeal",
  components: { Pagination },
  mixins: [Button],
  data() {
    return {
      tableKey: 0,
      list: [],
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        userAccount: undefined
      },
      total: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getUserList(this.listQuery).then(res => {
        console.log(res);
        this.list = res.data.records;
        this.total = res.data.total;
        if (this.list.length > 1) {
          this.list.shift();
        }
        this.listLoading = false;
      });
    },
    handleDelete(row, index) {
      deleteUser(row.id).then(() => {
        this.list.splice(index, 1);
        this.$notify({
          title: "操作成功",
          message: "已删除",
          type: "success",
          duration: 2000
        });
      });
    }
  }
};
</script>
